
angular.module('app')
    .controller('communitychannelController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '频道配置';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/channel/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/channel/read/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }


            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };


            $scope.search();

            $scope.clearSearch = function() {

                $scope.searchParams= { };
                $scope.search();
                $(".editSortBtn").show();
                $(".saveSortBtn").hide();
            }



            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };


            $scope.businessLicenses = [];
            $scope.preview = function(businessLicense){
                if (businessLicense != null && businessLicense != '') {
                    $scope.businessLicenses = businessLicense.split('|');
                }
                $scope.firstApply = true;
            }

            // 删除用户
            $scope.enableOrdisabled=function (channelNo,enable) {
                $scope.alertModel({
                    text:'确定此操作？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/channel/enableOrdisabled',
                        data: {channelNo:channelNo,enable:enable}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.search();
                    });

                })

            }

            // 拖拽排序
            $scope.editSort=function ($event) {
                if($('#menuName').val()){
                    $scope.alertModal({'content':'名称为空方可拖拽排序'});
                    return;
                }

                $("#channelList").sortable({opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40});
                $("#channelList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    $(".editSortBtn").hide();
                    $("#sortId").hide();
                    $(".saveSortBtn").show();
                }
            }

            $scope.saveSort=function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                    var tr = $(".sort  tbody tr")[j];
                    var id = $($(tr).find("td")[1]).html();
                    var sort = j + 1;
                    sortArray[j] = sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    // alert(batchUpdate);
                    batchUpdates[j] = batchUpdate;
                }
                $.ajax({
                    url: $scope.app.project_name + '/channel/editSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $("#sortId").show();
                        $(".saveSortBtn").hide();
                        $("#channelList").sortable('destroy');//解除拖拽
                        $timeout(function () {
                            $scope.search();
                        }, 1000);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

            $scope.operEnable = function (channelNo,$event) {
                $.ajax({
                    url : $scope.app.project_name+'/channel/updateStatus',
                    data: {channelNo:channelNo,updateType:"enable",enable:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

            $scope.operIsFixed = function (channelNo,$event) {
                $.ajax({
                    url : $scope.app.project_name+'/channel/updateStatus',
                    data: {channelNo:channelNo,updateType:"isFixed",enable:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }




        } ]);